package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService{


    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private RegionServiceImpl regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public Destination get(String id) {
        Destination destination = destinationRepository.findById(id).orElse(null);
        return destination;
    }

    /**
     * 查询关联目的地数据
     * @return
     */
    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    /**
     * 根据区域id查找目的地集合
     * @param rid
     * @return
     */
    @Override
    public List<Destination> getDestByRegionId(String rid) {
        //先根据rid查关联地区的id
        Region region = regionService.get(rid);
        //根据地区对象查询字段
        List<String> refIds = region.getRefIds();
        //在目的地表中根据区域id查出目的地
        List<Destination> destinationList = destinationRepository.findByIdIn(refIds);
        return destinationList;
    }

    @Override
    public Page<Destination> queryPageList(DestinationQuery qo) {
       /* Query query = new Query();
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }
        else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        long count = template.count(query, Destination.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        Query query1 = query.with(pageable);
        List<Destination> list = template.find(query1, Destination.class);

        return new PageImpl<Destination>(list,pageable,count);*/
        Query query = new Query();
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }else if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }
        else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,Destination.class,query,pageable);
    }

    /**
     * 吐司查询
     * @param qo
     * @return
     */
    @Override
    public List<Destination> getToast(DestinationQuery qo) {
        /*qo.getParentId()*/
        //自定义一个集合
        ArrayList<Destination> list = new ArrayList<>();
        //判断有没有父id
        if(!StringUtils.hasLength(qo.getParentId())){
            //如果没有就直接输出他的list
            return list;
        }
        //有的话说明不是最高级
        //所以调用递归方法,传入他的集合和父id
        this.getToastList(list,qo.getParentId());
        Collections.reverse(list);
        return list;
    }
    public void getToastList(List<Destination> list,String parentId){
        //根据他的父id获取目的地对象
        Destination destination = this.get(parentId);
        //将这个对象放进集合中
        list.add(destination);
        //判断他有没有父id
        if(StringUtils.hasLength(destination.getParentId())){
            getToastList(list,destination.getParentId());
        }
    }


    /**
     * 根据区域id查询目的地
     * @param regionId
     * @return
     */
    @Override
    public List<Destination> searchByRegionId(String regionId) {
       /* //第一层
        if(regionId.equals("-1")){
            //根据父名称查找目的地集合
            List<Destination> destList = destinationRepository.findByParentName("中国");
            for (Destination destination : destList) {
                //设置查询数据的个数,利用分页的方式
                //第二层
                Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
                List<Destination> list = destinationRepository.findByParentId(pageable,destination.getId());
                destination.setChildren(list);
            }
            return destList;
        }else {
            List<Destination> destList = this.getDestByRegionId(regionId);
            for (Destination destination : destList) {
                Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
                List<Destination> list = destinationRepository.findByParentId(pageable, destination.getId());
                destination.setChildren(list);
            }
            return destList;
        }*/
        List<Destination> list = new ArrayList<>();
        //第一层
        if(regionId.equals("-1")){
           list = destinationRepository.findByParentName("中国");
        }else{
           list = this.getDestByRegionId(regionId);
        }
        //第二层
        for (Destination destination : list) {
            Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> list1 = destinationRepository.findByParentId(pageable, destination.getId());
            destination.setChildren(list1);
        }
        return list;
    }

    @Override
    public List<Destination> getToasts(String destId) {
        List<Destination> list = new ArrayList<>();
        this.getToastList(list,destId);
        Collections.reverse(list);
        return list;
    }

    @Override
    public Destination queryByName(String name) {
        Destination dest = destinationRepository.findByName(name);
        return dest;
    }


}
